package com.todo.strategy.online.application.frontend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.enums.BizErrorCodeEnum;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.online.application.convert.StrategyConvert;
import com.todo.strategy.online.application.entity.bo.TravelFriendRelationBO;
import com.todo.strategy.online.application.frontend.service.TravelFriendRelationService;
import com.todo.strategy.online.infrastructure.entity.po.TravelFriendRelationPO;
import com.todo.strategy.online.infrastructure.repository.TravelFriendRelationRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (TravelFriendRelation)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:49
 */
@Slf4j
@Service("travelFriendRelationService")
public class TravelFriendRelationServiceImpl implements TravelFriendRelationService {

    @Resource
    private TravelFriendRelationRepository travelFriendRelationRepository;

    @Override
    public Boolean insertTravelFriendRelation(TravelFriendRelationBO travelFriendRelationBO) {
        if (travelFriendRelationBO == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        List<TravelFriendRelationBO> travelFriendRelationBOList = queryTravelFriendRelationByPlanId(travelFriendRelationBO.getPlanId());
        if (CollectionUtils.isNotEmpty(travelFriendRelationBOList)) {
            TravelFriendRelationBO alreadyData = travelFriendRelationBOList
                    .stream()
                    .filter(item -> item.getFriendId().equals(travelFriendRelationBO.getFriendId()))
                    .findFirst()
                    .orElse(null);
            if (alreadyData != null) {
                log.info("TravelFriendRelationService#insertTravelFriendRelation 新增出行好友关联完成，存在已知记录，默认成功 travelFriendRelationBO：【{}】", JSONObject.toJSONString(travelFriendRelationBO));
                return true;
            }
        }
        TravelFriendRelationPO travelFriendRelationPO = StrategyConvert.convertToTravelFriendRelationPO(travelFriendRelationBO);
        Boolean insertResult = travelFriendRelationRepository.insertTravelFriendRelationPO(travelFriendRelationPO);
        return insertResult;
    }

    @Override
    public Boolean updateTravelFriendRelation(TravelFriendRelationBO travelFriendRelationBO) {
        if (travelFriendRelationBO == null || travelFriendRelationBO.getId() == null) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        TravelFriendRelationBO alreadyData = queryTravelFriendRelationById(travelFriendRelationBO.getId());
        if (alreadyData == null) {
            log.error("TravelFriendRelationService#updateTravelFriendRelation 更新出行好友关联失败，数据查询为空，不存在此记录 travelFriendRelationBO：【{}】", JSONObject.toJSONString(travelFriendRelationBO));
            throw new BizException(BizErrorCodeEnum.DATA_NOT_EXIST);
        }
        TravelFriendRelationPO travelFriendRelationPO = StrategyConvert.convertToTravelFriendRelationPO(travelFriendRelationBO);
        Boolean updateResult = travelFriendRelationRepository.updateTravelFriendRelationPOById(travelFriendRelationPO);
        return updateResult;
    }

    @Override
    public Boolean removeTravelFriendRelationById(Long id) {
        if (id == null || id < 0) {
            throw new BizException(BizErrorCodeEnum.PARAM_ERROR);
        }
        TravelFriendRelationBO travelFriendRelationBO = queryTravelFriendRelationById(id);
        if (travelFriendRelationBO == null) {
            log.info("TravelFriendRelationService#removeTravelFriendRelationById 删除出行好友关联失败，数据查询为空，默认删除成功 travelFriendRelationBO：【{}】", JSONObject.toJSONString(travelFriendRelationBO));
            return true;
        }
        Boolean removeResult = travelFriendRelationRepository.removeTravelFriendRelationPOById(id);
        return removeResult;
    }

    @Override
    public TravelFriendRelationBO queryTravelFriendRelationById(Long id) {
        if (id == null || id < 0) {
            return null;
        }
        TravelFriendRelationPO travelFriendRelationPO = travelFriendRelationRepository.queryTravelFriendRelationPOById(id);
        if (travelFriendRelationPO == null) {
            log.warn("TravelFriendRelationService#queryTravelFriendRelationById 根据主键查询还有关联信息完成，数据为空 id:【{}】", id);
            return null;
        }
        return StrategyConvert.convertToTravelFriendRelationBO(travelFriendRelationPO);
    }

    @Override
    public List<TravelFriendRelationBO> queryTravelFriendRelationByPlanId(Long planId) {
        if (planId == null || planId < 0) {
            return Collections.emptyList();
        }
        List<TravelFriendRelationPO> travelFriendRelationPOList = travelFriendRelationRepository.queryTravelFriendRelationListByPlanId(planId);
        if (CollectionUtils.isEmpty(travelFriendRelationPOList)) {
            log.warn("TravelFriendRelationService#queryTravelFriendRelationByPlanId 根据计划Id查询所有关联好友列表完成，数据为空 planId：【{}】", planId);
            return Collections.emptyList();
        }
        return travelFriendRelationPOList.stream().map(StrategyConvert::convertToTravelFriendRelationBO).collect(Collectors.toList());
    }

    @Override
    public List<TravelFriendRelationBO> queryJoinTravelRelationListByFriendId(Long friendId) {
        if (friendId == null || friendId < 0) {
            return Collections.emptyList();
        }
        List<TravelFriendRelationPO> travelFriendRelationPOList = travelFriendRelationRepository.queryJoinTravelRelationListByFriendId(friendId);
        if (CollectionUtils.isEmpty(travelFriendRelationPOList)) {
            log.warn("TravelFriendRelationService#queryJoinTravelRelationListByFriendId 根据好友Id查询所有关联出行列表完成，数据为空 friendId：【{}】", friendId);
            return Collections.emptyList();
        }
        return travelFriendRelationPOList.stream().map(StrategyConvert::convertToTravelFriendRelationBO).collect(Collectors.toList());
    }

}
